﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GUI
{
    public partial class FormDKLichVanChuyen : Form
    {
        public static DateTime ngDi;
        public static String mChuyen;
        public static String sHieu;
        public static int flag;
        public int row;

        public FormDKLichVanChuyen()
        {
            InitializeComponent();
        }

        private void FormDKLichVanChuyen_Load(object sender, EventArgs e)
        {
            if (FormDangNhap.Loai == "2")
                btnDangKy.Enabled = false;
            List<LoaiXeDTO> lx = LoaiXeBUS.SelectALLLoaiXe();            
            foreach (var item in lx)
            {
                this.cbbLoaiXe.Items.Add(item.MALOAI+"_"+item.HANGSX);
            }
            List<String> mc = ChuyenXeBUS.SelectALLMaChuyen();
            foreach (var item in mc)
            {
                this.cbbChuyen.Items.Add(item);
            }
            this.cbbChuyen.SelectedItem = mc[0].ToString();
            this.cbbLoaiXe.SelectedItem = lx[0].MALOAI+"_"+lx[0].HANGSX;
            this.rbtTatCa.Checked = true;
            this.dataGridView1.DataSource = LichDiBUS.SelectALLLichDi();
            ngDi = DateTime.Parse(dataGridView1.Rows[0].Cells[7].Value.ToString());
            mChuyen = dataGridView1.Rows[0].Cells[0].Value.ToString();
            sHieu = dataGridView1.Rows[0].Cells[1].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Columns.Clear();
            if (rbtLoaiXe.Checked == true)
            {
                String[] ll = cbbLoaiXe.Text.ToString().Split('_');
                String xe = ll[0];
                this.dataGridView1.DataSource = LichDiBUS.SelectLichDiByMaLoai(xe);
            }
            else
            {
                if (rbtLichDi.Checked == true)
                {
                    String machuyen = this.cbbChuyen.Text.ToString();
                    this.dataGridView1.DataSource = LichDiBUS.SelectLichDiByMaChuyen(machuyen);
                }
                else
                {
                    FormDKLichVanChuyen_Load(null,null);
                }
            }
        }

        private void rbtLoaiXe_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridView1.Columns.Clear();
            if (rbtLoaiXe.Checked == true)
            {
                this.cbbLoaiXe.Enabled = true;
                this.cbbChuyen.Enabled = false;
                this.dtpNgay.Enabled = false;
                this.cbbLoaiXe.Focus();
            }
        }

        private void rbtLichDi_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridView1.Columns.Clear();
            if (rbtLichDi.Checked == true)
            {
                this.cbbChuyen.Enabled = true;
                this.cbbLoaiXe.Enabled = false;
                this.dtpNgay.Enabled = false;
                this.cbbChuyen.Focus();
            }
        }

        private void rbtNgay_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridView1.Columns.Clear();
            if (rbtNgay.Checked == true)
            {
                this.cbbChuyen.Enabled = false;
                this.cbbLoaiXe.Enabled = false;
                this.dtpNgay.Enabled = true;
                this.dtpNgay.Focus();

            }
        }

        private void rbtTatCa_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridView1.Columns.Clear();
            this.cbbChuyen.Enabled = false;
            this.cbbLoaiXe.Enabled = false;
            this.dtpNgay.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.CurrentRow.Index;
            mChuyen = dataGridView1.Rows[row].Cells[0].Value.ToString();
            sHieu = dataGridView1.Rows[row].Cells[1].Value.ToString();
            ngDi = Convert.ToDateTime(dataGridView1[7, row].Value);
        }

        private void btnDangKy_Click(object sender, EventArgs e)
        {
            FormDangKyLichDi frm = new FormDangKyLichDi();
            frm.ShowDialog();
        }
    }
}
